package edu.mtu.citizenscience.streamapplication.activity.main;

import edu.mtu.citizenscience.streamapplication.activity.DBHelper;
import edu.mtu.citizenscience.streamapplication.activity.R;
import edu.mtu.citizenscience.streamapplication.activity.R.layout;
import edu.mtu.citizenscience.streamapplication.activity.R.menu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class SCTreatmentsActivity extends Activity {
	
	public static final String ROW_ID = "ROW_ID";
	private Long rowID;
	private DBHelper myDbHelper;
	
	private Cursor report;
	
	private EditText textC1;
	private EditText textC2;
	private EditText textC3;
	private EditText textC4;
	private EditText textC5;
	private EditText textC6;
	private EditText textC7;
	private EditText textC8;
	private EditText textC9;
	private EditText textC10;
	private EditText textC11;
	private EditText textC12;
	private EditText textC13;
	private EditText textReason;
	private RadioGroup rGroup1;
	private RadioGroup rGroup2;
	private String revegString, bridgeString;
	private RadioButton selectedRadioR, selectedRadioB;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sctreatments);
		myDbHelper = new DBHelper(this);
		myDbHelper.open();
		
		textC1 = (EditText) findViewById(R.id.editText1);
		textC2 = (EditText) findViewById(R.id.editText2);
		textC3 = (EditText) findViewById(R.id.editText3);
		textC4 = (EditText) findViewById(R.id.editText4);
		textC5 = (EditText) findViewById(R.id.editText5);
		textC6 = (EditText) findViewById(R.id.editText6);
		textC7 = (EditText) findViewById(R.id.editText7);
		textC8 = (EditText) findViewById(R.id.editText8);
		textC9 = (EditText) findViewById(R.id.editText9);
		textC10 = (EditText) findViewById(R.id.editText10);
		textC11 = (EditText) findViewById(R.id.editText11);
		textC12 = (EditText) findViewById(R.id.editText12);
		textC13 = (EditText) findViewById(R.id.editText13);
		textReason = (EditText) findViewById(R.id.treatmentReasonText);
		rGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
		rGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
		
		rowID = this.getIntent().getLongExtra(this.ROW_ID, -1);
		populateFields();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sctreatments, menu);
		return true;
	}

	private void populateFields() {
    	report = myDbHelper.fetchReport(rowID, "Treatment");
    	
        if (report.getCount() == 1) {
            textC1.setText(""+report.getInt(report.getColumnIndexOrThrow("pavement_ft")));
            textC2.setText(""+report.getInt(report.getColumnIndexOrThrow("paved_curb_gutter_ft")));
            textC3.setText(""+report.getInt(report.getColumnIndexOrThrow("erosion_control_struct")));
            textC4.setText(""+report.getInt(report.getColumnIndexOrThrow("sediment_basins")));
            textC5.setText(""+report.getInt(report.getColumnIndexOrThrow("replace_culv")));
            textC6.setText(""+report.getInt(report.getColumnIndexOrThrow("replace_culv_dia")));
            textC7.setText(""+report.getInt(report.getColumnIndexOrThrow("replace_culv_length")));
            textC8.setText(""+report.getInt(report.getColumnIndexOrThrow("extend_culv")));
            textC9.setText(""+report.getInt(report.getColumnIndexOrThrow("extend_culv_ft")));
            textC10.setText(""+report.getInt(report.getColumnIndexOrThrow("diversion_outlets")));
            textC11.setText(""+report.getInt(report.getColumnIndexOrThrow("increase_fill")));
            textC12.setText(""+report.getInt(report.getColumnIndexOrThrow("place_rock_riprap_ft")));
            textC13.setText(""+report.getInt(report.getColumnIndexOrThrow("place_rock_riprap_yd")));
            
            //textReason.setText(report.getString(report.getColumnIndexOrThrow("")));
            
            
            //compare conditionRadios to string in database and select matching
            revegString = report.getString(report.getColumnIndexOrThrow("revegetation")); 
            if(revegString.equals("None"))
            	rGroup1.check(R.id.radioRNone);
            else if(revegString.equals("Small job"))
            	rGroup1.check(R.id.radioRSmall);
            else if(revegString.equals("Large job"))
            	rGroup1.check(R.id.radioRLarge);
            else
            	rGroup1.check(-1);
            
          //compare conditionRadios to string in database and select matching
            bridgeString = report.getString(report.getColumnIndexOrThrow("bridge")); 
            if(bridgeString.equals("None"))
            	rGroup2.check(R.id.radioBNone);
            else if(bridgeString.equals("Small job"))
            	rGroup2.check(R.id.radioBSmall);
            else if(bridgeString.equals("Large job"))
            	rGroup2.check(R.id.radioBLarge);
            else
            	rGroup2.check(-1);
          
        }
        else
        {
        	rGroup1.check(-1);
        	rGroup2.check(-1);
        }
    }
    
    private void saveLocation(){

    	String selectedR1String, selectedR2String;
    	
		if((textC1.getText().toString()).length()==0)
			textC1.setText("0");
		if((textC2.getText().toString()).length()==0)
			textC2.setText("0");
		if((textC3.getText().toString()).length()==0)
			textC3.setText("0");
		if((textC4.getText().toString()).length()==0)
			textC4.setText("0");
		if((textC5.getText().toString()).length()==0)
			textC5.setText("0");
		if((textC6.getText().toString()).length()==0)
			textC6.setText("0");
		if((textC7.getText().toString()).length()==0)
			textC7.setText("0");
		if((textC8.getText().toString()).length()==0)
			textC8.setText("0");
		if((textC9.getText().toString()).length()==0)
			textC9.setText("0");
		if((textC10.getText().toString()).length()==0)
			textC10.setText("0");
		if((textC11.getText().toString()).length()==0)
			textC11.setText("0");
		if((textC12.getText().toString()).length()==0)
			textC12.setText("0");
		if((textC13.getText().toString()).length()==0)
			textC13.setText("0");
		//if((textReason.getText().toString()).length()==0)
			//textReason.setText("None");
		
		selectedRadioR = (RadioButton) findViewById(rGroup1.getCheckedRadioButtonId());
		selectedRadioB = (RadioButton) findViewById(rGroup2.getCheckedRadioButtonId());
		
		if(rGroup1.getCheckedRadioButtonId() > 0)
			selectedR1String=selectedRadioR.getText().toString();
		else
			selectedR1String="Empty";
		
		if(rGroup2.getCheckedRadioButtonId() > 0)
			selectedR2String=selectedRadioB.getText().toString();
		else
			selectedR2String="Empty";
		
		if(report.getCount() == 0){
			
			myDbHelper.createTreatment(rowID, Integer.parseInt(textC1.getText().toString()), Integer.parseInt(textC2.getText().toString()), Integer.parseInt(textC3.getText().toString()), 
					Integer.parseInt(textC4.getText().toString()), Integer.parseInt(textC5.getText().toString()), Integer.parseInt(textC6.getText().toString()), Integer.parseInt(textC7.getText().toString()), 
					Integer.parseInt(textC8.getText().toString()), Integer.parseInt(textC9.getText().toString()), Integer.parseInt(textC10.getText().toString()), Integer.parseInt(textC11.getText().toString()), 
					Integer.parseInt(textC12.getText().toString()), Integer.parseInt(textC13.getText().toString()), selectedR1String, selectedR2String);
		}
		else{
			myDbHelper.updateTreatment(rowID, Integer.parseInt(textC1.getText().toString()), Integer.parseInt(textC2.getText().toString()), Integer.parseInt(textC3.getText().toString()), 
					Integer.parseInt(textC4.getText().toString()), Integer.parseInt(textC5.getText().toString()), Integer.parseInt(textC6.getText().toString()), Integer.parseInt(textC7.getText().toString()), 
					Integer.parseInt(textC8.getText().toString()), Integer.parseInt(textC9.getText().toString()), Integer.parseInt(textC10.getText().toString()), Integer.parseInt(textC11.getText().toString()), 
					Integer.parseInt(textC12.getText().toString()), Integer.parseInt(textC13.getText().toString()), selectedR1String, selectedR2String);
		}
    }
    
	/** Called when the user clicks the Continue button */
	public void cont(View view) {
		saveLocation();
		myDbHelper.close();
	    
		this.finish();
	}
	
	public void cancel(View view) {
		myDbHelper.close();
	    
		this.finish();
	}
	
}
